<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2020/8/21
 * Time: 16:02
 */
namespace App\Api\Controllers\MeiTuanPeiSong;


use App\Models\MeituanpsConfigs;
use App\Models\MeituanpsOrders;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class SelectController extends BaseController
{

    //查询订单状态
    public function orderStatusQuery(Request $request)
    {
        $token = $this->parseToken();
        $store_id = $request->get('storeId', '');
        $delivery_id = $request->get('deliveryId', '');
        $mt_peisong_id = $request->get('mtPeisongId', '');

        $obj = MeituanpsConfigs::where('store_id', $store_id)
            ->first();
        if (!$obj) {
            return json_encode([
                'status' => '2',
                'message' => '美团配送参数未配置'
            ]);
        }

        $order = MeituanpsOrders::where('delivery_id', $delivery_id)
            ->where('mt_peisong_id', $mt_peisong_id)
            ->first();
        if (!$order) {
            return json_encode([
                'status' => '2',
                'message' => '美团配送订单不存在'
            ]);
        }

        $secret = $obj->secret;

        try {
            $requestData = [
                'delivery_id' => $delivery_id, //配送活动标识
                'mt_peisong_id' => $mt_peisong_id //美团配送内部订单id，最长不超过32个字符
            ];
            Log::info('美团配送-查询订单状态');
            Log::info($requestData);
            $res = $this->http_post($this->order_status_query, $requestData, $secret);
            Log::info($res);
            if ($res['status'] == '1') {
//                if ($order->status != 20) {
                    $updateData = [
                        'status' => $res['data']['status']
                    ];
                    if (isset($res['data']['operate_time'])) $updateData['operate_time'] = date('Y-m-d H:i:s', $res['data']['operate_time']);
                    $result = $order->update($updateData);
//                }
            }

            return $res;
        } catch (\Exception $ex) {
            return json_encode([
                'status' => '-1',
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
            ]);
        }
    }


//    //
//    public function orderCreateByShop22(Request $request)
//    {
//        $token = $this->parseToken();
//        $config_id = $request->get('config_id', '');
//
//        $obj = MeituanpsConfigs::where('config_id', $config_id)
//            ->first();
//        if (!$obj) {
//            return json_encode([
//                'status' => '2',
//                'message' => '美团配送参数未配置'
//            ]);
//        }
//
//        $secret = $obj->secret;
//
//        try {
//            $requestData = [
//                '' => $, //
//                '' => $, //
//            ];
//            $res = $this->http_post($this->url, $requestData, $secret);
//            return $res;
//        } catch (\Exception $ex) {
//            return json_encode([
//                'status' => '-1',
//                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
//            ]);
//        }
//    }


}
